import Base from '@/utils/base.js'

let imagestatic = Base.requestUrl('IMAGE_STATIC')
let tabbarPage = ['/', '/pages/index/index', '/pages/waitwork/waitlist']

module.exports = {
	data() {
		return {
			imagestatic,
			tabbarPage
		}
	},
	methods: {
		/**
		 * 为空判断
		 * {},[],'',undefined,'undefined',null,'null'
		 * @param v
		 * @returns {boolean}
		 */
		isEmpty(v) {
			if (v && typeof v === 'object') {
				return Object.keys(v).length === 0;
			} else if (Array.isArray(v)) {
				return v.length === 0;
			}
			return v === '' || v === 'undefined' || v === undefined || v === null || v === 'null';
		},
		async FormVali(data) {
			let result = await (Base.formvali(data));
			if (!result.type) {
				uni.showToast({
					title: result.msg + '不能为空',
					icon: 'none'
				})
				return false
			} else return true;
		},
		navto(url, type = 'nav') {
			if(this.tabbarPage.includes(url)){
				uni.switchTab({
					url
				})
			}
			if (type == 'reLaunch') {
				uni.reLaunch({
					url
				})
			} else if (type == 'redirectTo') {
				uni.redirectTo({
					url
				})
			} else {
				uni.navigateTo({
					url
				})
			}
		},
		hrefto(url) {
			if (url.indexOf('/pages') != -1) {
				url = url.slice(url.indexOf('/pages'), url.length);
				uni.navigateTo({
					url
				})
			} else {
				// #ifndef MP-WEIXIN
				Base.getUrl(url) ? location.href = url : '';
				// #endif
				// #ifndef H5
				uni.navigateTo({
					url
				})
				// #endif
			}
		},
		QsData(data) {
			let newdata = {};
			for (var i in data) {
				newdata[i] = data[i].value;
			}
			return newdata;
		},
		uniback() {
			// #ifdef H5
			if (this.$pages.prePage()) {
				uni.navigateBack();
			} else {
				window.history.go(-1)
			}
			// #endif
			// #ifndef H5
			uni.navigateBack();
			// #endif	
		},
		//打开预览office文件
		handleopenfile(url, type = false) {
			if (type) {
				window.open(url);
			} else {
				let checkurl = url.toLowerCase();
				if (checkurl.endsWith('docx') || checkurl.endsWith('doc') || checkurl.endsWith('xlsx') || checkurl.endsWith('xls') || checkurl.endsWith('pdf')) {
					if (checkurl.endsWith('pdf')) {
						window.open('http://pdf.b.sxlian.net/?file=' + encodeURIComponent(url));
						return;
					}
					let fileurl = encodeURIComponent(url);
					let openurl = 'http://view.officeapps.live.com/op/view.aspx?src=' + fileurl;
					window.open(openurl, '_target');
				} else if (checkurl.endsWith('jpg') || checkurl.endsWith('jpeg') || checkurl.endsWith('png')) {
					uni.previewImage({
						current: 0,
						indicator: 'number',
						urls: [url],
					})
				} else {
					this.$base.msg('暂不支持查看');
				}
			}
		}
	}
}